/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecipeEditor.java
 *
 * Created on 2011-04-09, 13:34:13
 */
package other;

import cookbookdesktop.CookbookDesktopView;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import model.Cookbook;
import model.Recipe;
import org.jdesktop.application.Action;

/**
 *
 * @author zimi
 */
public class RecipeEditor extends javax.swing.JFrame {

    Cookbook cookbook = XMLUtils.getCookbook();
    CookbookDesktopView cookbookDesktopView;
    /** Creates new form RecipeEditor */
    public RecipeEditor(CookbookDesktopView cookbookDesktopView) {
        initComponents();
        this.cookbookDesktopView = cookbookDesktopView;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jEditorPaneOpis = new javax.swing.JEditorPane();
        jTextFieldNazwa = new javax.swing.JTextField();
        jTextFieldKategoria = new javax.swing.JTextField();
        jLabelOpis = new javax.swing.JLabel();
        jLabelNazwa = new javax.swing.JLabel();
        jLabelKategoria = new javax.swing.JLabel();
        jTextFieldIloscPorcji = new javax.swing.JTextField();
        jLabelIloscPorcji = new javax.swing.JLabel();
        jCheckBoxUlubione = new javax.swing.JCheckBox();
        jCheckBoxRecently = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jTextFieldZdjecie = new javax.swing.JTextField();
        jLabelImage = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabelZdjecie = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cookbookdesktop.CookbookDesktopApp.class).getContext().getResourceMap(RecipeEditor.class);
        setTitle(resourceMap.getString("Edytor.title")); // NOI18N
        setBackground(resourceMap.getColor("Edytor.background")); // NOI18N
        setIconImages(null);
        setName("Edytor"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setMinimumSize(new java.awt.Dimension(805, 374));
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jEditorPaneOpis.setName("jEditorPaneOpis"); // NOI18N
        jScrollPane3.setViewportView(jEditorPaneOpis);

        jTextFieldNazwa.setText(resourceMap.getString("jTextFieldNazwa.text")); // NOI18N
        jTextFieldNazwa.setName("jTextFieldNazwa"); // NOI18N

        jTextFieldKategoria.setText(resourceMap.getString("jTextFieldKategoria.text")); // NOI18N
        jTextFieldKategoria.setName("jTextFieldKategoria"); // NOI18N

        jLabelOpis.setText(resourceMap.getString("jLabelOpis.text")); // NOI18N
        jLabelOpis.setName("jLabelOpis"); // NOI18N

        jLabelNazwa.setText(resourceMap.getString("jLabelNazwa.text")); // NOI18N
        jLabelNazwa.setName("jLabelNazwa"); // NOI18N

        jLabelKategoria.setText(resourceMap.getString("jLabelKategoria.text")); // NOI18N
        jLabelKategoria.setName("jLabelKategoria"); // NOI18N

        jTextFieldIloscPorcji.setText(resourceMap.getString("jTextFieldIloscPorcji.text")); // NOI18N
        jTextFieldIloscPorcji.setName("jTextFieldIloscPorcji"); // NOI18N

        jLabelIloscPorcji.setText(resourceMap.getString("jLabelIloscPorcji.text")); // NOI18N
        jLabelIloscPorcji.setName("jLabelIloscPorcji"); // NOI18N

        jCheckBoxUlubione.setBackground(resourceMap.getColor("jCheckBoxUlubione.background")); // NOI18N
        jCheckBoxUlubione.setText(resourceMap.getString("jCheckBoxUlubione.text")); // NOI18N
        jCheckBoxUlubione.setName("jCheckBoxUlubione"); // NOI18N

        jCheckBoxRecently.setBackground(resourceMap.getColor("jCheckBoxRecently.background")); // NOI18N
        jCheckBoxRecently.setText(resourceMap.getString("jCheckBoxRecently.text")); // NOI18N
        jCheckBoxRecently.setName("jCheckBoxRecently"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNazwa)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelKategoria)
                            .addComponent(jLabelOpis))
                        .addGap(25, 25, 25)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldKategoria, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                            .addComponent(jTextFieldNazwa, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jCheckBoxUlubione)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jCheckBoxRecently))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabelIloscPorcji)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldIloscPorcji, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 239, Short.MAX_VALUE)))
                .addGap(36, 36, 36))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jLabelNazwa)
                    .addComponent(jTextFieldNazwa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelKategoria)
                    .addComponent(jTextFieldKategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelOpis)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelIloscPorcji)
                    .addComponent(jTextFieldIloscPorcji, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBoxUlubione)
                    .addComponent(jCheckBoxRecently))
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cookbookdesktop.CookbookDesktopApp.class).getContext().getActionMap(RecipeEditor.class, this);
        jButton1.setAction(actionMap.get("saveRecipeAndClose")); // NOI18N
        jButton1.setBackground(resourceMap.getColor("jButton1.background")); // NOI18N
        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton4.setAction(actionMap.get("editActivities")); // NOI18N
        jButton4.setBackground(resourceMap.getColor("jButton4.background")); // NOI18N
        jButton4.setFont(resourceMap.getFont("jButton4.font")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        jButton3.setAction(actionMap.get("editIngedients")); // NOI18N
        jButton3.setBackground(resourceMap.getColor("jButton3.background")); // NOI18N
        jButton3.setFont(resourceMap.getFont("jButton3.font")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jPanel4.setBackground(resourceMap.getColor("jPanel4.background")); // NOI18N
        jPanel4.setName("jPanel4"); // NOI18N

        jTextFieldZdjecie.setText(resourceMap.getString("jTextFieldZdjecie.text")); // NOI18N
        jTextFieldZdjecie.setName("jTextFieldZdjecie"); // NOI18N

        jLabelImage.setBackground(resourceMap.getColor("jLabelImage.background")); // NOI18N
        jLabelImage.setForeground(resourceMap.getColor("jLabelImage.foreground")); // NOI18N
        jLabelImage.setText(resourceMap.getString("jLabelImage.text")); // NOI18N
        jLabelImage.setName("jLabelImage"); // NOI18N

        jButton2.setAction(actionMap.get("chooseImage")); // NOI18N
        jButton2.setBackground(resourceMap.getColor("jButton2.background")); // NOI18N
        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jLabelZdjecie.setText(resourceMap.getString("jLabelZdjecie.text")); // NOI18N
        jLabelZdjecie.setName("jLabelZdjecie"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelImage, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabelZdjecie)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldZdjecie, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelZdjecie)
                    .addComponent(jTextFieldZdjecie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelImage, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .addGap(22, 22, 22))
        );

        jButton6.setAction(actionMap.get("removeRecipe")); // NOI18N
        jButton6.setBackground(resourceMap.getColor("jButton6.background")); // NOI18N
        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton6))
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton6)
                            .addComponent(jButton3)
                            .addComponent(jButton4))
                        .addGap(4, 4, 4)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        JFrame frame = (JFrame) evt.getSource();
        frame.setVisible(false);
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        JFrame frame = (JFrame) evt.getSource();
        frame.setVisible(false);
    }//GEN-LAST:event_formWindowClosed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton6;
    private javax.swing.JCheckBox jCheckBoxRecently;
    private javax.swing.JCheckBox jCheckBoxUlubione;
    private javax.swing.JEditorPane jEditorPaneOpis;
    private javax.swing.JLabel jLabelIloscPorcji;
    private javax.swing.JLabel jLabelImage;
    private javax.swing.JLabel jLabelKategoria;
    private javax.swing.JLabel jLabelNazwa;
    private javax.swing.JLabel jLabelOpis;
    private javax.swing.JLabel jLabelZdjecie;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTextFieldIloscPorcji;
    private javax.swing.JTextField jTextFieldKategoria;
    private javax.swing.JTextField jTextFieldNazwa;
    private javax.swing.JTextField jTextFieldZdjecie;
    // End of variables declaration//GEN-END:variables
    private Recipe actualRecipe;

    public void use(Recipe recipe) {
        actualRecipe = recipe;
        showActualRecipe(actualRecipe);
    }

    @Action
    public void saveRecipeAndClose() {
        savetRecipe();
        cookbookDesktopView.refresh();
        this.setVisible(false);
    }

    @Action
    public void savetRecipe() {
        //walidacja

        //tworzenie

        actualRecipe.setCategory(jTextFieldKategoria.getText());
        actualRecipe.setDescription(jEditorPaneOpis.getText());
        actualRecipe.setFavorite(jCheckBoxUlubione.isSelected());
        actualRecipe.setName(jTextFieldNazwa.getText());
        actualRecipe.setPicturePath(jTextFieldZdjecie.getText());
        try {
            actualRecipe.setPortions(Integer.valueOf(jTextFieldIloscPorcji.getText()));
        } catch (Exception ex) {
            //TODO
        }
        actualRecipe.setRecently(jCheckBoxRecently.isSelected());

        int size = cookbook.listRecipes().size();

        for (int i = 0; i < size; i++) {
            Recipe recipe = (Recipe) cookbook.listRecipes().elementAt(i);
            if (recipe.getName().equals(actualRecipe.getName())) {
                cookbook.listRecipes().remove(recipe);
                break;
            }
        }


        cookbook.addRecipe(actualRecipe);
        XMLUtils.writeToXml(cookbook);


    }
    JFileChooser jFileChooser = new JFileChooser();

    @Action
    public void chooseImage() throws IOException {
        jFileChooser.setVisible(true);
        int returns = jFileChooser.showOpenDialog(jTextFieldZdjecie);
        if (returns == JFileChooser.APPROVE_OPTION) {

            //String targetDir = System.getProperty("user.dir");
            File tmp = new File(XMLUtils.path);
            String targetDir = tmp.getParent() + File.separator + "pics";
            File targetFileDir = new File(targetDir);
            if(!targetFileDir.exists()){
                if(targetFileDir.mkdir() == false) {
                    throw new IOException("FileCopy: " + "cannnot create dir: " + targetDir);
                }
            }
            String targetName = File.separator + jFileChooser.getSelectedFile().getName();
            //System.out.println(targetDir);

            String target = targetDir + targetName;
            File targetFile = new File(target);
            int i = 0;
            while(targetFile.exists()){
                int index = targetName.lastIndexOf(".");
                String newName = targetName.substring(0, index)+ "_" + String.valueOf(i);
                String extension = targetName.substring(index, targetName.length());
                target = targetDir + newName + extension;
                targetFile = new File(target);
                i += 1;
            }
            FILEUtils.copy(jFileChooser.getSelectedFile().getPath(), target);

            String oldPicture = actualRecipe.getPicturePath();
            actualRecipe.setPicturePath(new File(target).getName());
            System.out.println(new File(target).getName());

            BufferedImage image = ImageIO.read(targetFile);
            double scale = (double)jLabelImage.getWidth()/(double)image.getWidth();

            if((int)(image.getHeight()*scale) > jLabelImage.getHeight()) {
                scale = (double)jLabelImage.getHeight()/(double)image.getHeight();
            }
            jLabelImage.setIcon(new ImageIcon(image.getScaledInstance((int)(image.getWidth()*scale),  (int)(image.getHeight()*scale), java.awt.Image.SCALE_SMOOTH)));
            jTextFieldZdjecie.setText(new File(target).getName());
            jLabelImage.setText("");

            if(!oldPicture.equalsIgnoreCase(actualRecipe.getPicturePath())){
                File oldFile = new File(oldPicture);
                oldFile.delete();
            }
        }

    }

    private void showActualRecipe(Recipe actualRecipe) {
        jTextFieldNazwa.setText(actualRecipe.getName());
        try {

            jTextFieldKategoria.setText(actualRecipe.getCategory());
        } catch (NullPointerException e) {
            jTextFieldKategoria.setText("");
            //brak opisow
        }
        jTextFieldIloscPorcji.setText(String.valueOf(actualRecipe.getPortions()));
        jEditorPaneOpis.setText(actualRecipe.getDescription());
        jTextFieldZdjecie.setText(actualRecipe.getPicturePath());
        BufferedImage image;
        if(actualRecipe.getPicturePath() != null) {
            try {
                System.out.println(new File(XMLUtils.path).getParent() + File.separator + "pics" + File.separator + actualRecipe.getPicturePath());
                image = ImageIO.read(new File(new File(XMLUtils.path).getParent() + File.separator + "pics" + File.separator + actualRecipe.getPicturePath()));
                //jLabelImage.setIcon(new ImageIcon(image));
                double scale = (double)jLabelImage.getWidth()/(double)image.getWidth();

                if((int)(image.getHeight()*scale) > jLabelImage.getHeight()) {
                    scale = (double)jLabelImage.getHeight()/(double)image.getHeight();
                }
                jLabelImage.setIcon(new ImageIcon(image.getScaledInstance((int)(image.getWidth()*scale),  (int)(image.getHeight()*scale), java.awt.Image.SCALE_SMOOTH)));

                jLabelImage.setText("");
            } catch (IOException ex) {
                jLabelImage.setIcon(null);
                jLabelImage.setText("Cannot load image.");
                //Logger.getLogger(RecipeEditor.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            jLabelImage.setIcon(null);
        }
        jCheckBoxRecently.setSelected(actualRecipe.isRecently());
        jCheckBoxUlubione.setSelected(actualRecipe.isFavorite());

    }
    RecipeEditorActivities recipeEditorActivites = new RecipeEditorActivities();

    @Action
    public void editIngedients() {
        RecipeEditorIngredients recipeEditorIngredients = new RecipeEditorIngredients(actualRecipe,this);
        recipeEditorIngredients.setVisible(true);
    }

    @Action
    public void editActivities() {
        RecipeEditorActivities recipeEditorActivities = new RecipeEditorActivities(actualRecipe,this);
        recipeEditorActivities.setVisible(true);
    }

    @Action
    public void removeRecipe() {
//        cookbook.removeRecipe(actualRecipe);
        if(cookbook.listRecipes().findRecipe(actualRecipe) != null){
            cookbook.listRecipes().remove(actualRecipe);
            XMLUtils.writeToXml(cookbook);
            cookbookDesktopView.refresh();
        }
        this.setVisible(false);

    }
}
